import React, { useEffect, useRef, useState } from 'react'
import favorites from '../../assets/icon/favorites.png'
import hFavorites from '../../assets/icon/hFavorites.png'
import styles from './index.less'
import { useHistory, useDispatch, useSelector } from 'dva'
import { E } from '../../../../constants'
import defaultListIcon from '../../assets/news/defaultListIcon.png'
import moment from 'moment'
import { getToken } from '../../utils/authority'
function Index({ obj,type }) {
  const history = useHistory()
  const dispatch = useDispatch()
  const {
    userInfo
  } = useSelector(({ index }) => index)

  function updateLike(id) {
    // console.log(id, 'iiiiiiiiiiiiii')
    if (!userInfo) {
      dispatch({
        type: 'index/setState',
        payload: {
          loginModalShow: true
        }
      })
      return
    }
    dispatch({
      type: 'index/apiPostLikeNews',
      payload: {
        objectId: id,
        objectType: 'ARTICLE',
        type: 'COLLECTED',
        userId: userInfo?.user?.id,
        operateType: type
      }
    })
  }
  return (
    <div className={styles.itemCard} onClick={(e) => {
      e.stopPropagation()
      // let token = getToken()
      // if (!token) {
      //   dispatch({
      //     type: 'index/setState',
      //     payload: {
      //       loginModalShow: true
      //     }
      //   })
      //   return
      // }
      if(obj.linkType === 'EXTERNAL_LINK') {
        window.open(obj.linkContent)
        return
      }
      if(obj.linkType === 'RICH_TEXT') {
        history.push(`/news/details/${obj && obj.id}`)
      }
    }}>
      {
        <div className={styles.content}>
          <div>
            {/*<img src={obj && obj.picture}></img>*/}
          </div>
          <div className={styles.textBox}>
            <span className={styles.title} style={{ WebkitBoxOrient: "vertical", MozBoxOrient: 'vertical' }}>{obj && obj.name}</span>
          </div>
        </div>
      }
      <div className={styles.date}>
        <span>{ obj.updateTime ? moment(obj.updateTime).format('YYYY-MM-DD') : moment(obj.createTime).format('YYYY-MM-DD')}</span>
        <img src={obj && obj.ifCollect ? hFavorites : favorites} alt="isHot"
             onClick={(e) => {
               e.stopPropagation()
               updateLike(obj.id)
             }}></img>
      </div>
    </div>
  )
}

export default Index
